<?php

namespace app\controllers;

use app\models\Park;
use app\models\SignupForm;
use app\models\Test;
use app\models\UploadForm;
use app\models\UserLog;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\Url;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\web\UploadedFile;

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        $this->layout="login";
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            $this->layout="main";

            if($model->type==1) {
                return $this->redirect(Url::toRoute('/frontend/site/index'));
            }
            if($model->type==2){
                return $this->redirect(Url::toRoute('/backend/site/index'));
            }
        } else {
            return $this->render('index', [
                'model' => $model,
            ]);
        }
        return $this->render('index',['model'=>$model]);
    }

    public function actionUser()
    {
        return $this->render('user');
    }
    /*public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }*/
	public function actionLogin()
    {
        $this->layout="login";
         if (!\Yii::$app->user->isGuest) {
             return $this->goHome();
         }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            $this->layout="main";

            if($model->type==1) {
                $parks = Park::find()->all();
                return $this->render('index',['parks'=>$parks]);
            }
            if($model->type==2){
                return $this->redirect(Url::toRoute('/backend/site/index'));
            }
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    public function actionLogout()
    {

        Yii::$app->user->logout();
        $model = new LoginForm();
        return $this->redirect('index.php');

    }

    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    public function actionAbout()
    {
        return $this->render('about');
    }
    public function actionTest()
    {
        return $this->render('test');
    }

    public function actionSignup()
    {
        $model = new SignupForm();
         if ($model->load(Yii::$app->request->post())) {
             if($model->signup()){
                 return $this->render('submit_success');
             }
         }

        /*if ($model->load(Yii::$app->request->post())) {

            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
            $model->imageFile2 = UploadedFile::getInstance($model, 'imageFile2');

            if ($model->upload()) {
                var_dump("文件上传成功");
                return;
            }
        }*/
        return $this->render('signup', [
            'model' => $model,
        ]);
        }

    public function actionParklist($id){
        $countParks = Park::find()
            ->where(['company_id' => $id])
            ->count();
        $parks = Park::find()
            ->where(['company_id' => $id])
            ->all();
        if ($countParks> 0) {
            foreach ($parks as $park) {
                echo "<option value='" . $park->id ."'>" . $park->name . "</option>";
        }
        } else {
            echo "<option>-</option>";
        }
    }

    public function actionUploadimg(){
        $params=Yii::$app->request->post();
        $model = new UploadForm();
        if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstance($model, 'file');
            if ($model->file && $model->validate()) {
                if(!file_exists(Yii::$app->basePath.'/uploads/tmp/'.time())) mkdir(Yii::$app->basePath.'/uploads/tmp/'.time());
                $basePath = '/shbb/uploads/tmp/'.time().'/';
                $path=Yii::$app->basePath.'/uploads/tmp/'.time().'/';
                if(!file_exists($path))mkdir($path);

                $filename=$model->file->baseName.'_'.time().'.' . $model->file->extension;
                if($model->file->saveAs($path.$filename))
                    return json_encode(["result"=>"Success","url"=>$basePath.$filename]);

                else return json_encode(["result"=>"Fail"]);
            }
            return json_encode(["result"=>"ValidFail"]);
        }
        return json_encode(["result"=>"PostFail"]);
    }

    public function actionSubmitSuccess(){
        return $this->render('submit_success');
    }



}
